﻿using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace QueryData;

public class 预加载数据
{
    void MyMethod()
    {
        using (var db = new BasicDbContext())
        {
            db.MainEntities
               .Include(m => m.SubEntities.Where(x => x.MainEntityId > 5))
                   .ThenInclude(s => s.SubSubEntities.OrderByDescending(x => x.Time).Take(3))
               // 此处的Include没有附加表达式，但是会使用上一个相同导航的Include附加的表达式
               .Include(m => m.SubEntities)
                   .ThenInclude(s => s.SubSubEntities2)
               .Include(m => m.SubEntities2)
               .ToList();

            db.MainEntities
                .Include(m => m.SubEntities.Where(x => x.MainEntityId > 5))
                    .ThenInclude(s => s.SubSubEntities.OrderByDescending(x => x.Time).Take(3))
                // 此处的Include附加了和上一个相同导航相同的表达式，如果要附加，请务必确保相同 
                .Include(m => m.SubEntities.Where(x => x.MainEntityId > 5))
                    .ThenInclude(s => s.SubSubEntities2)
                .Include(m => m.SubEntities2)
                .ToList();


        }
    }

}
